package com.fenqing.leetcode.code.t600_700;

import com.fenqing.annotations.DesignClass;
import com.fenqing.core.design.DesignTopic;

/**
 * @author fenqing
 */
public class Topic622 extends DesignTopic {

    public Topic622() {
        //
        //[null,true,true,-1,false,-1,-1,true,false,true,-1,true]
        //
        super(
                "[\"MyCircularQueue\",\"enQueue\",\"deQueue\",\"Front\",\"deQueue\",\"Front\",\"Rear\",\"enQueue\",\"isFull\",\"deQueue\",\"Rear\",\"enQueue\"]",
                "[[3],[7],[],[],[],[],[],[0],[],[],[],[3]]");
    }

    public static void main(String[] args) {
        System.out.println(new Topic622().run());
    }

    @DesignClass
    public static class MyCircularQueue {

        int[] nums;

        int size, p, q;


        public MyCircularQueue(int k) {
            nums = new int[k];
            size = 0;
            p = 0;
            q = 1;
        }


        private int nextIndex(int index) {
            if (index + 1 < nums.length) {
                return index + 1;
            } else {
                return 0;
            }
        }

        private int prevIndex(int index) {
            if (index - 1 >= 0) {
                return index - 1;
            } else {
                return nums.length - 1;
            }
        }

        public boolean enQueue(int value) {
            if (!isFull()) {
                nums[prevIndex(this.q)] = value;
                q = nextIndex(this.q);
                size++;
                return true;
            }
            return false;
        }

        public boolean deQueue() {
            if (isEmpty()) {
                return false;
            }
            p = nextIndex(this.p);
            size--;
            return true;
        }

        public int Front() {
            if (isEmpty()) {
                return -1;
            }
            return nums[p];
        }

        public int Rear() {
            if (isEmpty()) {
                return -1;
            }
            int i = prevIndex(this.q);
            int index = prevIndex(i);
            return nums[index];
        }

        public boolean isEmpty() {
            return size == 0;
        }

        public boolean isFull() {
            return size == nums.length;
        }
    }


}
